#include <Editor/StdAfx.h>
#include <Editor/EditorApp.h>
#include <Editor/MainFrame.h>

using namespace fastbird;
GlobalEnv* g_Env=0;

//----------------------------------------------------------------------------
bool EditorApp::OnInit()
{
	mNumMonitors = 0;
	wxSize size(800, 600);
	wxPoint pos = CalcMainFramePos(size);

	MainFrame* frame = new MainFrame(_("fastbird Editor"), pos, size );
	frame->Show(true);
	SetTopWindow(frame);

	wxString dllName;
	dllName = _("./Engine");
#ifdef _DEBUG 
	dllName +=_("_Debug");
#endif
	dllName += _(".dll");
	HINSTANCE hDLL = LoadLibrary(dllName.c_str());
	if (!hDLL)
	{
		assert(0);
	}
	else
	{
		typedef fastbird::IEngine* (_cdecl *CREATE_ENGINE_PROC)(void);
		CREATE_ENGINE_PROC CreateEngineProc = (CREATE_ENGINE_PROC)GetProcAddress(hDLL, "CreateEngine");
		if (CreateEngineProc == 0)
		{
			assert(0);
		}
		else
		{
			{
				fastbird::EnginePtr pEngine = CreateEngineProc();
				pEngine->GetGlobalEnv(&g_Env);
			}
		}
	}
	
	return true;
}

//----------------------------------------------------------------------------
BOOL CALLBACK MonitorEnumProc(
  _In_  HMONITOR hMonitor,
  _In_  HDC hdcMonitor,
  _In_  LPRECT lprcMonitor,
  _In_  LPARAM dwData
)
{
	EditorApp* pApp = (EditorApp*)dwData;
	pApp->mNumMonitors++;
	pApp->mMonitorRect = *lprcMonitor;
	if (pApp->mNumMonitors==2)
		return FALSE;

	return TRUE;
}

//----------------------------------------------------------------------------
wxPoint EditorApp::CalcMainFramePos(const wxSize& size)
{	
	EnumDisplayMonitors(0, 0, MonitorEnumProc, (LPARAM)this);
	if (mNumMonitors>0)
	{
		wxPoint position( mMonitorRect.left + (mMonitorRect.right - mMonitorRect.left)/2,
			mMonitorRect.top + (mMonitorRect.bottom - mMonitorRect.top) / 2);
		position.x -= size.x/2;
		position.y -= size.y/2;
		return position;
	}
	return wxPoint();
}

IMPLEMENT_APP(EditorApp)